/**
 * 构造函数
 */
var LBEventDispatcher   = function () {

    this._eventListenerMap      = {};
    this._eventTriggerQueue     = [];
    this._workerTerminateTimes  = 0;
    this._workerPostTimes       = 0;
    this._workerPostMaxTimes    = 100000;
};

LBEventDispatcher.getInstance   = function () {

    if ('undefined' == typeof LBEventDispatcher._instance) {

        (LBEventDispatcher._instance = new LBEventDispatcher).initialize();
    }

    return  LBEventDispatcher._instance;
}

/**
 * 注册事件
 *
 * @param   LBEvent event       事件
 * @param   Object  listener    监听器
 * @param   int     priority    优先级
 */
LBEventDispatcher.prototype.addEventListener    = function (event, listener, priority) {

    if (!event.getName()) {

        return  false;
    }

    if ('undefined' == typeof this._eventListenerMap[event.getName() + event.getID()]) {

        this._eventListenerMap[event.getName() + event.getID()] = [];
    }

    this._eventListenerMap[event.getName() + event.getID()].push({
        "event"     : event,
        "listener"  : listener,
        "priority"  : priority
    });
};

/**
 * 注册事件
 */
LBEventDispatcher.prototype.removeEventListener    = function (event, listener, priority) {

    if (!event.getName()) {

        return  false;
    }

    if ('undefined' == typeof this._eventListenerMap[event.getName() + event.getID()]) {

        return  false;
    }

    var isPop   = false;
    var newList = [];

    for (var offset = (this._eventListenerMap[event.getName() + event.getID()].length - 1);offset >= 0 ;offset --) {

        if (
            false == isPop &&
            priority == this._eventListenerMap[event.getName() + event.getID()][offset].priority &&
            listener === this._eventListenerMap[event.getName() + event.getID()][offset].listener
        ) {

            isPop   = true;
            break;
        }
    }

    if (isPop) {

        this._eventListenerMap[event.getName() + event.getID()].splice(offset, 1);

        return  true;
    }

    return  false;
};

/**
 * 触发事件
 */
LBEventDispatcher.prototype.triggerEvent        = function (event) {

    if (!event.getName()) {

        return  false;
    }

    this._eventTriggerQueue.push(event);
    this._handler.postMessage();
};

/**
 * 触发事件
 */
LBEventDispatcher.prototype.execTriggerEvent    = function (event) {

    if (!event.getName()) {

        return  ;
    }

    if ('undefined' == typeof this._eventListenerMap[event.getName() + event.getID()]) {

        return  ;
    }

    for (var offset = 0;offset < this._eventListenerMap[event.getName() + event.getID()].length;offset ++) {

        if ('function' == typeof this._eventListenerMap[event.getName() + event.getID()][offset].listener) {

            this._eventListenerMap[event.getName() + event.getID()][offset].listener(event);
        }
    }
};

/**
 * 初始化
 */
LBEventDispatcher.prototype.initialize          = function () {


    this._handler           = new Worker('lib/core/EventDispatcher/LBEventLoop.js');
    var me                  = this;
    this._handler.onmessage = function () {

        if (me._workerPostTimes >= me._workerPostMaxTimes) {

            var handler = me._handler.onmessage;
            ++ me._workerTerminateTimes;
            me._workerPostTimes = 0;
            this.terminate();
            me._handler = null;
            me._handler = new Worker('lib/core/EventDispatcher/LBEventLoop.js');
            me._handler.onmessage   = handler;
        }

        ++ me._workerPostTimes;

        if (me._eventTriggerQueue.length > 0) {

            me.execTriggerEvent(me._eventTriggerQueue.shift());
        }

        return  ;
    };
};
